<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="90px">
      <el-form-item label="机器人名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="机器人名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['robot:bot:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table border v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="机器人名字" align="center" prop="name"/>
      <el-table-column label="机器人类型" align="center" prop="botType"/>
      <el-table-column label="机器人token" align="center" prop="botToken"/>
      <el-table-column label="管理群组" align="center">
        <template slot-scope="scope">
          <el-button type="primary" plain @click="manageGroups(scope.row)">查看管理群组</el-button>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value: false
            inactive-value: true
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column fixed="left" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['robot:bot:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['robot:bot:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改公司信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="机器人名字" prop="name">
          <el-input v-model="form.name" placeholder="机器人名字"/>
        </el-form-item>
        <el-form-item label="机器人类型" prop="botType">
          <el-input v-model="form.botType" placeholder="机器人类型"></el-input>
        </el-form-item>
        <el-form-item label="机器人token" prop="botToken">
          <el-input v-model="form.botToken" placeholder="机器人token"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.name }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <template v-model="BOTGROUPObj.open">
      <el-dialog :title="BOTGROUPObj.title" :visible.sync="BOTGROUPObj.open" width="500px"
                 :before-close="closeBOTGROUPObj"
                 close-on-click-modal  modal  append-to-body>
        <BOTGROUP :botId="BOTGROUPObj.botId" :open="BOTGROUPObj.open"/>
      </el-dialog>
    </template>
  </div>
</template>

<script>
import {add, changeBotStatus, del, getList, getOneById, update} from "@/api/bot/bot";
import BOTGROUP from "@/views/robot/bot/botgroup.vue";


export default {
  components: {BOTGROUP},
  name: "bot",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      BOTGROUPObj: {
        // 点击群组的行机器人id
        botId: undefined,
        title: "",
        open: undefined,
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 机器人信息表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
      },
      statusOptions: [
        {name: "启用", value: true},
        {name: "停用", value: false}
      ],
      // 表单参数
      form: {
        status: false
      },
      // 表单校验
      rules: {
        name: [
          {required: true, message: "机器人名不能为空", trigger: "blur"},
        ],
        botType: [
          {required: true, message: "机器类型不能为空", trigger: "blur"},
        ],
        botToken: [
          {required: true, message: "机器token不能为空", trigger: "blur"},
        ],
        status: [
          {required: true, message: "状态必须选择", trigger: "blur"},
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公司信息列表 */
    getList() {
      this.loading = true;
      getList(this.queryParams).then(response => {
        this.list = response.records;
        this.total = response.count;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        bot_type: undefined,
        bot_token: undefined,
        status: undefined,
        createdTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机器人";
    },

    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === true ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.name + '"机器人吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return changeBotStatus(row.id, row.status);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === false ? "1" : "0";
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOneById(id).then(response => {
        this.form = response;
        this.open = true;
        this.title = "修改机器人信息";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            update(this.form).then(response => {
              if (response === "OK") {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            add(this.form).then(response => {
              if (response === "OK") {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除公司信息编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return del(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () {
      });
    },
    /**  机器人管理群组  */
    manageGroups(row) {
      this.BOTGROUPObj.botId = row.id
      this.BOTGROUPObj.title = "机器人(" + row.name + ")所在群组"
      this.BOTGROUPObj.open = true
    },
    closeBOTGROUPObj(done){
      this.BOTGROUPObj.open = false
    },
  }
};
</script>
